home *** CD-ROM | disk | FTP | other *** search
/ 9-Digit Zip Code Directory / 9-Digit Zip Code Directory (American Business Information) (ABIZIP-12).ISO / z4src.zip / BSTITLE.C < prev    next >
C/C++ Source or Header  |  1995-04-30  |  11KB  |  414 lines

  1. //----------------------------------------------------------------------------
  2. //                            MODULE DESCRIPTION
  3. //
  4. //  Module:    bstitle.c
  5. //   Title:    Base library
  6. //  Notice:    John M. Weeder
  7. //                 Copyright (c) 1993. All rights reserved.
  8. //             This module contains proprietary information and should be 
  9. //                treated as confidential.
  10. //
  11. //----------------------------------------------------------------------------
  12. //                           MAINTENANCE HISTORY
  13. //
  14. // $Workfile$
  15. // $Revision$
  16. //   $Author$
  17. //     $Date$
  18. //      $Log$    
  19. //
  20. //----------------------------------------------------------------------------
  21. //                             MODULE NARRATIVE
  22. //
  23. //
  24. //    This module contains .
  25. //
  26. //    The code in this module should be written entirely in C. 
  27. //    Do not use any C++ constructs.
  28. //
  29. //    This module is portable to:
  30. //        DOS 3.X+
  31. //        MS Windows 3.X+
  32. //        MS Windows NT
  33. //        OS/2 2.X+
  34. //        OS/2 2.0 PM
  35. //        SCO UNIX.
  36. //
  37. //    The following compilers are supported:
  38. //        MSC 6.0A
  39. //        MSC/C++ 7.0
  40. //        Borland C++ 3.1 for DOS
  41. //        Borland C++ 1.0 for OS/2 2.X
  42. //        SCO UNIX cc
  43. //
  44. //----------------------------------------------------------------------------
  45. #include <bs.h>
  46.  
  47.  
  48. //----------------------------------------------------------------------------
  49. //   Description:    Display program title string and other information
  50. //    Parameters:    pszRevision        PVCS revision string.
  51. //                        pcszDate         Compilation date (use __DATE__ macro)
  52. //                        pcszTime         Compilation time (use __DATE__ macro)
  53. //                        pcszName            Program descrptive name.
  54. //                                            If null, name is read from configuration 
  55. //                                            information structure.
  56. //                                            Default is NULL.
  57. //       Returns:    
  58. //----------------------------------------------------------------------------
  59. VOID FN_E BaseTitle(PSZ pszRevision, PCSZ pcszDate, PCSZ pcszTime, PCSZ pcszName)
  60. {
  61.     CHAR szFormat[512];
  62.     SIZET cb = 0;
  63.     PCSZ pcsz;
  64.  
  65.     szFormat[cb] = '\0';                        // Program name and version
  66.     pcsz = CfgGet(CFG_PROGRAM_NAME, NULL);
  67.     if (pcsz != NULL
  68.     && pcsz[0]
  69.     && stricmp(pcsz, CFG_PROGRAM_NAME_DFT) != 0)
  70.         pcszName = pcsz;
  71.     pcszName = pcszName == NULL ? CFG_PROGRAM_NAME_DFT: pcszName;
  72.     pcsz = CfgGet(CFG_VERSION, NULL);
  73.     cb += sprintf(szFormat + cb, "\n%s %s\n", pcszName, pcsz);
  74.  
  75. #if COMPILE_DEBUG
  76.     if (pszRevision)                            // Application name and PVCS revision
  77.         pszRevision = Revision(pszRevision);
  78.     else
  79.         pszRevision = "1.0";
  80.     cb += sprintf(szFormat + cb,
  81.         "%s v%s (%s)\n",
  82.         CommandLineAppName(), 
  83.         pszRevision,
  84.         CommandLineAppPath());
  85.  
  86.     if (pcszDate == NULL)                    // Compilation date/time
  87.         pcszDate = __DATE__;
  88.     if (pcszTime == NULL)
  89.         pcszTime = __TIME__;
  90.     cb += sprintf(szFormat + cb,
  91.         "Compiled on %s at %s.\n",
  92.         pcszDate, pcszTime);
  93.  
  94.     pcsz = Version(NULL,NULL);                // Operating system version
  95.     if (pcsz[0])
  96.         cb += sprintf(szFormat + cb, "%s\n", pcsz);
  97.  
  98.     {                                                // Program size
  99.     LONG lProgram, lFree;
  100.     ProgramSize(&lProgram, &lFree);
  101.     cb += sprintf(szFormat + cb,
  102.         "%ld/%ld K free (%ld K)\n",
  103.         (LONG)K(lFree), (LONG)K(lFree + lProgram), (LONG)K(lProgram));
  104.     }
  105.  
  106.     {
  107.     cb += sprintf(szFormat + cb,
  108.         "DEBUG mode is %s\n"
  109.         "LOGGING is %s, LOG LEVEL is %ld\n"
  110.         "SCREEN OUTPUT is %s, RUN LOG is %s\n",
  111.         (DebugMode() ? "enabled": "disabled"),
  112.         (LogEnabled() ? "enabled": "disabled"),
  113.         (LONG)LogGetLevel(),
  114.         (OutputScreen() ? "enabled": "disabled"),
  115.         (OutputRunLog() ? "enabled": "disabled"));
  116.     }
  117.  
  118.     {                                                // Id/access code/serial number
  119.     PCSZ pcszId     = CfgGet(CFG_PROGRAM_ID, NULL);
  120.     PCSZ pcszSerial = CfgGet(CFG_SERIAL_NO, NULL);
  121.     PCSZ pcszAccess = CfgGet(CFG_ACCESS, NULL);
  122.  
  123.     cb += sprintf(szFormat + cb, "ID=%-8s  SERIAL #=%-8s  ACCESS=%-8s\n",
  124.         pcszId, pcszSerial, pcszAccess);
  125.     }
  126.  
  127.     {
  128.     PCSZ pcszRegister  = CfgGet(CFG_REGISTER, NULL);
  129.     if (pcszRegister && pcszRegister[0])
  130.         cb += sprintf(szFormat + cb, "Registered to: %s\n", pcszRegister);
  131.     }
  132. #else
  133.     NOTUSED(pszRevision);
  134.     NOTUSED(pcszDate);
  135.     NOTUSED(pcszTime);
  136. #endif
  137.                                                     // Copyright
  138.     pcsz = CfgGet(CFG_COPYRIGHT, NULL);
  139.     if (pcsz[0])
  140.         cb += sprintf(szFormat + cb, "%s\n", pcsz);
  141.  
  142. #if OS_UNIX
  143.     strcat(szFormat, "/HELP for additional help.\n\n");
  144. #else
  145.     strcat(szFormat, "/? or /HELP for additional help.\n\n");
  146. #endif
  147.     Output(szFormat);
  148.     return ;
  149. }
  150.  
  151.  
  152. //----------------------------------------------------------------------------
  153. //   Description:    
  154. //    Parameters:
  155. //       Returns:    
  156. //----------------------------------------------------------------------------
  157. static VOID FN BaseTitleWrap(PSZ psz, SIZET cLen)
  158. {
  159. #define LINE_LEN        (79)
  160.  
  161.     BOOL fFirst = TRUE;
  162.  
  163.     if (cLen >= LINE_LEN || psz == NULL || !psz[0])
  164.         {
  165.         Output("\n");
  166.         return ;
  167.         }
  168.  
  169.     cLen = LINE_LEN - cLen;                    // Maximum line length
  170.     while (psz && psz[0])
  171.         {
  172.         CHAR szBuffer[80];
  173.         PSZ pszLf;
  174.  
  175.         if (!fFirst)                            // Indent
  176.             Output("%*s", LINE_LEN - cLen, "");
  177.  
  178.         pszLf = strchr(psz, '\n');
  179.         if (pszLf)                                // Copy up to next line feed
  180.             {
  181.             SIZET cLf = (SIZET)(pszLf - psz);
  182.  
  183.             strnzcpy(szBuffer, psz, MIN(cLen + 1, cLf + 1));
  184.             }
  185.         else                                        // Copy remainder of line
  186.             strnzcpy(szBuffer, psz, cLen + 1);
  187.  
  188.         Output("%s\n", szBuffer);            // Output line
  189.                                                     // Next line
  190.         psz = pszLf ? pszLf + 1: pszLf;
  191.         fFirst = FALSE;
  192.         }
  193.     return ;
  194. }
  195.  
  196.  
  197. //----------------------------------------------------------------------------
  198. //   Description:    
  199. //    Parameters:
  200. //       Returns:    TRUE if successful, else exit because of invalid command 
  201. //                        line or help requested.
  202. //----------------------------------------------------------------------------
  203. BOOL FN_E BaseTitleHelp(PBS_CMDOPT pcmdoptUser, PCSZ pcszDesc, FLAG16 fs)
  204. {
  205. static BOOL fHelp = FALSE;
  206. static BS_CMDOPT acmdoptHelp[] =
  207.     {
  208.     { "HELP",   (PVOID)&fHelp, CMDOPT_TRUE, "Display help."},
  209. #if OS_UNIX==0
  210.     { "?",        (PVOID)&fHelp, CMDOPT_TRUE|CMDOPT_NO_DFT},
  211. #endif
  212.     BS_CMDOPT_NULL
  213.     };
  214.     CHAR szOption[80];
  215.     CHAR szBuffer[80];
  216.     PBS_CMDOPT pcmdopt = NULL;
  217.     SIZET cOptions = 0;
  218.     SIZET cParms = 0;
  219.     SIZET cRequired = 0;
  220.     SIZET cEntries = 0;
  221.     SIZET i;
  222.     SIZET cWhich = 0;
  223.     SIZET cMax = 0;
  224.     CHAR szDefault[80];
  225.  
  226.     fHelp = FALSE;
  227.                                                     // Count local entries
  228.     for (i = 0; !NULOPT(acmdoptHelp + i); ++i)
  229.         {
  230.         if (BTEST(acmdoptHelp[i].fs, _CMDOPT_FILESPEC))
  231.             {
  232.             if (BTEST(acmdoptHelp[i].fs, CMDOPT_REQUIRED))
  233.                 cRequired++;
  234.             cParms++;
  235.             }
  236.         else
  237.             cOptions++;
  238.         cEntries++;
  239.         }
  240.     if (pcmdoptUser)                            // Count user entries
  241.         {
  242.         for (i = 0; !NULOPT(pcmdoptUser + i); ++i)
  243.             {
  244.             if (BTEST(pcmdoptUser[i].fs, _CMDOPT_FILESPEC))
  245.                 {
  246.                if (BTEST(pcmdoptUser[i].fs, CMDOPT_REQUIRED))
  247.                     cRequired++;
  248.                 cParms++;
  249.                 }
  250.             else
  251.                 cOptions++;
  252.             cEntries++;
  253.             }
  254.         }                                            // Allocate local structure
  255.     pcmdopt = (PBS_CMDOPT)MemAllocZero((cEntries + 1) * sizeof(BS_CMDOPT));
  256.     if (pcmdopt == NULL)
  257.         {
  258.         ErrorNoMem();
  259.         fHelp = TRUE;
  260.         goto ERROR_EXIT;
  261.         }
  262.     cEntries = 0;                                // Copy to local structure
  263.     for (i = 0; !NULOPT(acmdoptHelp + i); ++i, cEntries++)
  264.         pcmdopt[cEntries] = acmdoptHelp[i];
  265.     for (i = 0; !NULOPT(pcmdoptUser + i); ++i, cEntries++)
  266.         pcmdopt[cEntries] = pcmdoptUser[i];
  267.                                                     // Get maximum length of parm names
  268.     for (i = 0; !NULOPT(pcmdopt + i); ++i)
  269.         if (pcmdopt[i].psz)                
  270.             {
  271.             SIZET cLen = strlen(pcmdopt[i].psz);
  272.             cMax = MAX(cMax, cLen);
  273.             }
  274.     cMax = MIN(30, MAX(cMax, 15));
  275.                                                     // Process command line
  276.     if (pcmdopt)
  277.         {
  278.         if (!CommandLineProcess(pcmdopt, fs))
  279.             fHelp = TRUE;
  280.         cEntries = 0;                            // Transfer flags back
  281.         for (i = 0; !NULOPT(acmdoptHelp + i); ++i, cEntries++)
  282.             ;
  283.         for (i = 0; !NULOPT(pcmdoptUser + i); ++i, cEntries++)
  284.             pcmdoptUser[i].fs = pcmdopt[cEntries].fs;
  285.         }
  286.  
  287.     if (!fHelp)                                    // No help needed, so exit
  288.         goto ERROR_EXIT;                         // I apologize for the goto!!!*/
  289.  
  290.     Output("USAGE: %s ", CommandLineAppName());
  291.     if (cOptions)
  292.         Output( "[options] ");
  293.     if (cParms)
  294.         {
  295.        for (cWhich = i = 0; !NULOPT(pcmdopt + i); ++i)
  296.             {
  297.           if (BTEST(pcmdopt[i].fs, _CMDOPT_FILESPEC))
  298.                 {
  299.                 if (cWhich == cRequired)
  300.                     {
  301.                     SIZET j;
  302.                     for (j = 0; j < cParms - cRequired; ++j)
  303.                         Output("[");
  304.                     }
  305.                 ++cWhich;
  306.                 if (pcmdopt[i].psz)
  307.                     Output("%s", pcmdopt[i].psz);
  308.                 else
  309.                     Output("parm%d", cWhich);
  310.                 if (cWhich > cRequired)
  311.                     Output("] ");
  312.                 else
  313.                     Output(" ");
  314.                 }
  315.             }
  316.         }
  317.     Output("\n\n");
  318.  
  319.     if (pcszDesc)                                // Print program description if
  320.         {                                            //  requested!
  321.         Output("%s\n", pcszDesc);
  322.         Output("\n");
  323.         }
  324.  
  325.     if (cParms)                                    // Print help for parameters
  326.         {
  327.        Output("PARAMETERS:\n");
  328.        for (cWhich = i = 0; !NULOPT(pcmdopt + i); ++i)
  329.             {
  330.           if (BTEST(pcmdopt[i].fs, _CMDOPT_FILESPEC))
  331.                 {
  332.                 PSZ psz;
  333.                 CHAR szName[20];
  334.  
  335.                 ++cWhich;
  336.                 if (pcmdopt[i].psz)
  337.                     psz = pcmdopt[i].psz;
  338.                 else
  339.                     {
  340.                     sprintf(szName, "parm%d", cWhich);
  341.                     psz = szName;
  342.                     }
  343.                 sprintf(szBuffer,    "  %-*.*s  ", cMax, cMax, psz);
  344.                 Output("%s", szBuffer);
  345.                 BaseTitleWrap(pcmdopt[i].pszDesc, strlen(szBuffer));
  346.                 }
  347.             }
  348.         Output("\n");
  349.         }
  350.     if (cOptions)                                // Print help for options
  351.         {
  352.         Output("OPTIONS:\n");
  353.        for (cWhich = i = 0; !NULOPT(pcmdopt + i); ++i)
  354.             {
  355.             if (!BTEST(pcmdopt[i].fs, _CMDOPT_FILESPEC))
  356.                 {
  357.                 PSZ pszMod;
  358.                 SIZET cLen;
  359.  
  360.                 szDefault[0] = '\0';
  361.                 if (BTEST(pcmdopt[i].fs, (CMDOPT_BOOL|CMDOPT_SWITCH)))
  362.                     {
  363.                     if (pcmdopt[i].psz[0] == '?')
  364.                         pszMod = "";
  365.                     else if ((pcmdopt[i].fs & CMDOPT_TRUE) == CMDOPT_TRUE)
  366.                         pszMod = "[-]";
  367.                     else if ((pcmdopt[i].fs & CMDOPT_FALSE) == CMDOPT_FALSE)
  368.                         pszMod = "[+]";
  369.                     else
  370.                         pszMod = "";
  371.                     }
  372.                 else if (BTEST(pcmdopt[i].fs, CMDOPT_NUMERIC))
  373.                     {
  374.                     Assert(pcmdopt[i].pv);
  375.                     sprintf(szDefault, "Default is %ld", *(PLONG)pcmdopt[i].pv);
  376.                     pszMod = "=nnn";
  377.                     }
  378.                 else if (BTEST(pcmdopt[i].fs, _CMDOPT_TEXT))
  379.                     {
  380.                     Assert(pcmdopt[i].pv);
  381.                     sprintf(szDefault, "Default is '%s'", pcmdopt[i].pv);
  382.                     pszMod = "='text'";
  383.                     }
  384.                 else
  385.                     pszMod = "";
  386.  
  387.                 sprintf(szOption, "/%s%s", pcmdopt[i].psz, pszMod);
  388.                 sprintf(szBuffer,    "  %-*.*s  ", cMax, cMax, szOption);
  389.                 Output("%s", szBuffer);
  390.  
  391.                 cLen = strlen(szBuffer);
  392.                 BaseTitleWrap(pcmdopt[i].pszDesc, cLen);
  393.                 if (szDefault[0]
  394.                 && !BTEST(pcmdopt[i].fs, CMDOPT_NO_DFT))
  395.                     {
  396.                     Output("%*s%-*.*s\n",
  397.                         cLen, "",
  398.                         LINE_LEN - cLen, LINE_LEN - cLen, szDefault);
  399.                     }
  400.                 }
  401.             }
  402.         Output("\n");
  403.         }
  404.  
  405. ERROR_EXIT:
  406.     if (pcmdopt)
  407.         MemFree(pcmdopt);
  408.     return !fHelp;
  409.  
  410. }
  411. //----------------------------------------------------------------------------
  412. //------------------------------- End of File --------------------------------
  413. //----------------------------------------------------------------------------
  414.